package cn.org.qary.controller.yaokaiing.back;

import cn.org.qary.entity.FirstType;
import cn.org.qary.entity.SecondaryType;
import cn.org.qary.service.trait.yaokaiing.FirstTypeService;
import cn.org.qary.service.trait.yaokaiing.SecondaryTypeService;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Controller("back_TypeController")
@RequestMapping("back_type")
public class TypeController {
    @Autowired
    FirstTypeService ft;

    @Autowired
    SecondaryTypeService st;

    int rows=3;
    @RequestMapping("show/{page}")
    public String findAll(@PathVariable(required = false) Integer page,Model model){
        if (page==null) page=1;
        Page<Object> pages = PageHelper.startPage(page, rows);
        List<FirstType> list=ft.selectAll();
        model.addAttribute("firstType",list);

        model.addAttribute("pages",pages);
        return "manage/productClass";
    }

    /**
     * 为二级查询可选的一级分类
     */
    @RequestMapping("findFirstType")
    public String findFirstType(Model model){
        List<FirstType> firstTypes = ft.selectAll();
        model.addAttribute("firstTypes",firstTypes);
        return "manage/productClass-add";
    }
    /**
     * 通过一级分类id查询二级分类
     */
    @RequestMapping("findSecond/{id}")
    @ResponseBody
    public JSONObject findSecond(@PathVariable Integer id){
        Example condition=new Example(SecondaryType.class);
        Example.Criteria criteria = condition.createCriteria();
        criteria.andEqualTo("firstTypeId", id);
        List<SecondaryType> list =st.selectByCondition(condition);
        JSONObject json=new JSONObject();
        json.put("json1",list);
        return json;
    }

    /**
     * 后台接受用户选择的分类等级并记录
     */
    String firstorsecondarytype="0";
    @RequestMapping("gettype/{type}")
    @ResponseBody
    public String gettype(@PathVariable String type){
        firstorsecondarytype=type;
        return "";
    }
    /**
     * 用已获得的分类等级 来区分用户需要新增一级分类还是二级分类
     * 并新增一级分类或者 用获得的一级分类id新增二级分类
     */
    @RequestMapping("addtype")
    public String addtype(FirstType firstType){
        String type=firstorsecondarytype;
        if ("0".equals(type)){
            FirstType firstType1 =new FirstType(null,firstType.getName());
            ft.insert(firstType1);
        }else {
            SecondaryType secondaryType=new SecondaryType(null,firstType.getName(),Integer.valueOf(type));
            st.insert(secondaryType);
        }
        return "redirect:show/1";
    }

    @RequestMapping("delFirstType/{id}")
    public String delFirstType(@PathVariable Integer id){
        Example condition=new Example(SecondaryType.class);
        Example.Criteria criteria = condition.createCriteria();
        criteria.andEqualTo("firstTypeId", id);
        List<SecondaryType> list =st.selectByCondition(condition);
        for (SecondaryType secondaryType : list) {
            st.delete(secondaryType.getId());
        }
        ft.delete(id);
        return "redirect:/back_type/show/1";
    }
    @RequestMapping("delSecondaryType/{id}")
    public String delSecondaryType(@PathVariable Integer id){
        st.delete(id);
        return "redirect:/back_type/show/1";
    }

    /**
     * 用于修改前展示原一级分类属性 便于修改
     */
    @RequestMapping("showOneFirstType/{id}")
    public String showOneFirstType(@PathVariable Integer id,Model model){
        FirstType firstType=ft.selectById(id);
        model.addAttribute("firstType",firstType);
        return "manage/productClass-modify";
    }
    /**
     * 用于修改前展示原二级分类 以及其所属一级分类属性 便于修改
     */
    @RequestMapping("showOneSecondaryType/{id}/{firstTypeId}")
    public String showOneSecondaryType(@PathVariable Integer id,@PathVariable Integer firstTypeId,Model model){
        SecondaryType secondaryType=st.selectById(id);
        model.addAttribute("secondaryType",secondaryType);
        FirstType secondaryFirstType = ft.selectById(firstTypeId);
        model.addAttribute("secondaryFirstType",secondaryFirstType);
        return "manage/productClass-modify";
    }

    /**
     * 修改一二级分类属性
     */
    @RequestMapping("updateFirstType")
    public String updateFirstType(FirstType firstType){
        ft.update(firstType);
        return "redirect:show/1";
    }
    @RequestMapping("updateSecondaryType")
    public String updateSecondaryType(SecondaryType secondaryType){
        st.update(secondaryType);
        return "redirect:show/1";
    }


}
